package com.uniview.butterknifedemo;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import butterknife.BindArray;
import butterknife.BindString;
import butterknife.BindView;
import butterknife.BindViews;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnItemSelected;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();

    @BindView(R.id.am_tv)
    TextView textView;

    @BindView(R.id.progressBar)
    ProgressBar progressBar;

    @BindViews({R.id.ll_tv1, R.id.ll_tv2, R.id.ll_tv3})
    List<TextView> textViewList;

    @BindView(R.id.button)
    Button button;

    @BindViews({R.id.checkBox, R.id.checkBox2, R.id.checkBox3})
    List<CheckBox> checkBoxList;

    @BindViews({R.id.radioButton, R.id.radioButton2, R.id.radioButton3, R.id.radioButton4})
    List<RadioButton> radioButtonList;

    @BindView(R.id.spinner)
    Spinner spinner;

    @BindView(R.id.imageButton)
    ImageButton imageButton;


    @BindString(R.string.app_name)
    String appName;

    @BindArray(R.array.array)
    String[] arrays;

    @BindArray(R.array.array2)
    String[] arrays2;

    @BindArray(R.array.array3)
    String[] arrays3;

    static final ButterKnife.Setter<TextView, String[]> initialText = new ButterKnife.Setter<TextView, String[]>() {
        @Override
        public void set(@NonNull TextView view, String[] value, int index) {
            view.setText(value[index]);
        }
    };

    static final ButterKnife.Setter<TextView, String[]> initialRadio = new ButterKnife.Setter<TextView, String[]>() {
        @Override
        public void set(@NonNull TextView view, String[] value, int index) {
            view.setText(value[index]);
        }
    };

    static final ButterKnife.Action<TextView> visible = new ButterKnife.Action<TextView>() {
        @Override
        public void apply(@NonNull TextView view, int index) {
            view.setVisibility(View.VISIBLE);
        }
    };

    static final ButterKnife.Action<TextView> invisible = new ButterKnife.Action<TextView>() {
        @Override
        public void apply(@NonNull TextView view, int index) {
            view.setVisibility(View.INVISIBLE);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        initView();
    }

    private void initView() {
        textView.setText(appName);

//        for (int i = 0; i < 3; i++) {
//            textViewList.get(i).setText(arrays[i]);
//        }

        ButterKnife.apply(textViewList, initialText, arrays);
//        ButterKnife.apply(textViewList, actionText);
        ButterKnife.apply(checkBoxList, initialText, arrays2);
        ButterKnife.apply(radioButtonList, initialRadio, arrays3);

        ButterKnife.apply(textViewList, invisible);

        ArrayAdapter<CharSequence> spinnerAdapter = ArrayAdapter.createFromResource(this, android.R.array.organizationTypes, android.R.layout.simple_spinner_item);
        spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(spinnerAdapter);
    }

    @OnClick(R.id.am_tv)
    void textViewClick() {
        String text = textView.getText().toString();
        if (text.equals("Hello World")) {
            textView.setText("Hello zj");
        } else {
            textView.setText("Hello World");
        }
    }

    @OnClick({R.id.ll_tv1, R.id.ll_tv2, R.id.ll_tv3})
    void textMultipleClick() {
//        for (int i = 0; i < 3; i++) {
//            textViewList.get(i).setText(arrays2[i]);
//        }

        ButterKnife.apply(textViewList, initialText, arrays2);
    }

    @OnClick(R.id.button)
    void btnClick(View view) {
        int flag = progressBar.getVisibility();
        Log.e(TAG, "btnClick: flag = " + flag);
        if (flag == progressBar.VISIBLE) {
            progressBar.setVisibility(progressBar.INVISIBLE);
            ButterKnife.apply(textViewList, visible);
        } else {
            progressBar.setVisibility(progressBar.VISIBLE);
            ButterKnife.apply(textViewList, invisible);
        }
    }

    @OnClick({R.id.checkBox, R.id.checkBox2, R.id.checkBox3})
    void onCheckboxClicked(View view) {
        switch (view.getId()) {
            case R.id.checkBox:
                Toast.makeText(this, "you click checkbox + " + ((CheckBox) view).getText(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.checkBox2:
                Toast.makeText(this, "you click checkbox2 + " + ((CheckBox) view).getText(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.checkBox3:
                Toast.makeText(this, "you click checkbox3 + " + ((CheckBox) view).getText(), Toast.LENGTH_SHORT).show();
                break;
        }
    }

    @OnClick({R.id.radioButton, R.id.radioButton2, R.id.radioButton3, R.id.radioButton4})
    void onRadioClicked(View view) {
        switch (view.getId()) {
            case R.id.radioButton:
                Toast.makeText(this, "you click radiobutton + " + ((RadioButton) view).getText(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.radioButton2:
                Toast.makeText(this, "you click radiobutton2 + " + ((RadioButton) view).getText(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.radioButton3:
                Toast.makeText(this, "you click radiobutton3 + " + ((RadioButton) view).getText(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.radioButton4:
                Toast.makeText(this, "you click radiobutton4 + " + ((RadioButton) view).getText(), Toast.LENGTH_SHORT).show();
                break;
        }
    }

    @OnItemSelected(value = R.id.spinner, callback = OnItemSelected.Callback.ITEM_SELECTED)
    void onItemSel(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(this, "position: " + position + " id: " + id, Toast.LENGTH_SHORT).show();
    }

    @OnClick(R.id.imageButton)
    void onImageBtnClicked() {
        Intent intent = new Intent(this, Main3Activity.class);
        intent.putExtra("image", "image");
        startActivity(intent);
    }
}
